/**
 * Copyright 2011-2012 www.chinatvpay.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.shsh.safemanage.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.shsh.safemanage.model.Role;
import com.shsh.safemanage.util.ValueUtil;

public class RoleDao {
	public List<Role> getRoleList() {
		List<Role> roleList = new ArrayList<Role>();
		String sql = "select * from role";

		Connection conn = null;
		PreparedStatement pstm = null;
		try {
			conn = ConnectionManager.getSafeManageConn();
			pstm = conn.prepareStatement(sql);

			ResultSet rs = pstm.executeQuery();
			while (rs.next()) {
				Role role = new Role();
				role.setId(ValueUtil.getLong(rs.getLong("ID")));
				role.setRoleName(ValueUtil.getString(rs.getString("ROLE_NAME")));
				role.setRoleType(ValueUtil.getInteger(rs.getLong("ROLE_TYPE")));

				roleList.add(role);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (Exception e) {
			}
		}

		return roleList;
	}

	public boolean hasRight(Integer authType, Long roleID, Integer processMethod) {
		String sql = "select HAS_RIGHT from role_auth where AUTH_TYPE = ? and ROLE_ID=? and PROCESS_METHOD=?";
		Connection conn = null;
		PreparedStatement pstm = null;
		try {
			conn = ConnectionManager.getSafeManageConn();
			pstm = conn.prepareStatement(sql);

			pstm.setInt(1, authType);
			pstm.setLong(2, roleID);
			pstm.setInt(3, processMethod);

			ResultSet rs = pstm.executeQuery();
			if (rs.next()) {
				boolean hasRight = rs.getBoolean("HAS_RIGHT");
				return hasRight;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (Exception e) {
			}
		}
		return false;
	}

	public boolean hasRightByRoleType(Integer authType, Long roleType, Integer processMethod) {
		String sql = "select HAS_RIGHT from role_auth left join role on role.ID = role_auth.ROLE_ID where AUTH_TYPE = ? and role.ROLE_TYPE=? and PROCESS_METHOD=?";
		Connection conn = null;
		PreparedStatement pstm = null;
		try {
			conn = ConnectionManager.getSafeManageConn();
			pstm = conn.prepareStatement(sql);

			pstm.setInt(1, authType);
			pstm.setLong(2, roleType);
			pstm.setInt(3, processMethod);

			ResultSet rs = pstm.executeQuery();
			if (rs.next()) {
				boolean hasRight = rs.getBoolean("HAS_RIGHT");
				return hasRight;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (Exception e) {
			}
		}
		return false;
	}

	public Role getRoleByUserID(Long userID) {
		String sql1 = "select DEPARTMENT3_ID from employee where ID=?";
		String sql2 = "select ROLE_ID from department3 where ID=?";

		Connection conn = null;
		PreparedStatement pstm1 = null;
		PreparedStatement pstm2 = null;
		try {
			conn = ConnectionManager.getSafeManageConn();
			pstm1 = conn.prepareStatement(sql1);
			pstm1.setLong(1, userID);
			pstm2 = conn.prepareStatement(sql2);

			ResultSet rs1 = pstm1.executeQuery();
			if (rs1.next()) {
				Long depart3ID = rs1.getLong("DEPARTMENT3_ID");
				pstm2.setLong(1, depart3ID);

				ResultSet rs2 = pstm2.executeQuery();
				if (rs2.next()) {
					Long roleID = rs2.getLong("ROLE_ID");

					TitlePostManageGroupDao tpmDao = DaoCreator.createTitlePostManageGroupDao();
					return tpmDao.getRoleByID(roleID);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (Exception e) {
			}
		}

		return null;
	}
}
